// 1.保存effect
// 2. 检测值的改变
// 3. 执行trigger effect

const person = {
  name: 'viking'
}

const handler = {
  get(target, prop, receiver) {
    return Reflect.get(target, prop, receiver)
  },
  set(...args) {
    Reflect.set(...args)
  },
  has(target, prop, receiver) {
    return Reflect.has(target, prop, receiver)
  }
}

const proxy = new Proxy(person, handler);

// console.log('name' in proxy)



let product = {
  price: 5,
  count: 2,
}

let total = 0;

let effectArr = new Set();

function track() {
  effectArr.add(effect)
}

function trigger() {
  effectArr.forEach(cb =>{
    cb()
  })
}


function reactive1(obj) {
  const handler1 = {
    set() {
      // 先设置，再执行
      const res = Reflect.set(...arguments)
      trigger()
      return res
    },
    get(target, prop, receiver) {
      const res = Reflect.get(...arguments)
      track()
      return res
    }
  }
  return new Proxy(obj, handler1)
}

const ccc = reactive1(product)

let effect = () => {
  console.log(ccc)
  total = ccc.price * ccc.count;
}

effect()

console.log(total);
ccc.price = 10;
console.log(total)

